package com.centling.smartSealForPhone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.adapter.HistoryListAdapter;
import com.centling.smartSealForPhone.bean.HistoryListBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.widget.AutoRecyclerView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class HistoryActivity extends TitleBarActivity {

    private PtrClassicFrameLayout ptrHistoryList;
    private AutoRecyclerView rvHistoryList;
    private List<HistoryListBean.RowsBean> dataList = new ArrayList<>();

    private int pageIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        setTitleBarText("历史记录");
        setTitleBarRight(R.drawable.ic_history_search);
        setTitleBarRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(HistoryActivity.this, HistorySearchActivity.class));
            }
        });

        initView();

        initData();
    }

    private void initView() {
        ptrHistoryList = (PtrClassicFrameLayout) findViewById(R.id.ptr_history_list);
        rvHistoryList = (AutoRecyclerView) findViewById(R.id.rv_history_list);
    }

    private void initData() {
        rvHistoryList.setLayoutManager(new GridLayoutManager(mContext, 1));
        HistoryListAdapter listAdapter = new HistoryListAdapter(this, dataList);
        rvHistoryList.setAdapter(listAdapter);
        listAdapter.setOnClickListener(new HistoryListAdapter.OnClickListener() {
            @Override
            public void onClick(View v, int pos) {
                Intent intent = new Intent(HistoryActivity.this, HistoryDetailsActivity.class);
                intent.putExtra("sealApplyId", dataList.get(pos).getSealApplyId());
                startActivity(intent);
            }
        });

        ptrHistoryList.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                sendRequest(true);
            }
        });

        rvHistoryList.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                sendRequest(false);
            }
        });

        ptrHistoryList.post(new Runnable() {
            @Override
            public void run() {
                ptrHistoryList.autoRefresh();
            }
        });
    }

    private void sendRequest(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
        }
        getEnqueue(String.format(Locale.CHINA, UrlConstants.HISTORY_LIST_NEW, pageIndex), new
                OkHttpUtil.NetCallBack() {
                    @Override
                    public void onSucceed(String json) {
                        if (isRefresh) {
                            dataList.clear();
                            rvHistoryList.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
                                @Override
                                public void onLoadMore() {
                                    sendRequest(false);
                                }
                            });
                        }
                        HistoryListBean bean = new Gson().fromJson(json, HistoryListBean.class);
                        dataList.addAll(bean.getRows());
                        rvHistoryList.getAdapter().notifyDataSetChanged();
                        if (bean.getRows().size() < 20) {
                            rvHistoryList.loadMoreComplete(true);
                        } else {
                            rvHistoryList.loadMoreComplete(false);
                        }
                        ptrHistoryList.refreshComplete();
                        pageIndex++;
                    }

                    @Override
                    public void onFailed(String reason) {
                        ShowToast.shortTime(reason);
                        ptrHistoryList.refreshComplete();
                        rvHistoryList.loadMoreComplete(false);
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        rvHistoryList.post(new Runnable() {
            @Override
            public void run() {
                ptrHistoryList.autoRefresh();
            }
        });
    }
}
